home *** CD-ROM | disk | FTP | other *** search
- class someFrogs.Puzzles.SlidingPuzzle extends mx.core.UIComponent
- {
- var _picLinkage;
- var _hintStyle;
- var onEnterFrame;
- var piece_width;
- var rez_hrz;
- var piece_height;
- var rez_vrt;
- var emptySpace;
- var boundingBox_mc;
- var puzzlePic_mc;
- var loadListener;
- var controller;
- var border_mc;
- var frogPower_mc;
- var disabled_color;
- static var symbolName = "SlidingPuzzle";
- static var symbolOwner = someFrogs.Puzzles.SlidingPuzzle;
- var className = "SlidingPuzzle";
- var version = "2.0.0.0";
- var _puzzleRezH = 2;
- var _puzzleRezV = 2;
- var autoStart = true;
- var autoMix = true;
- var _puzzleDone = false;
- var _edgesArr = [];
- var useSignature = false;
- var top_depth = 10000;
- var stage_width = 100;
- var stage_height = 100;
- var pic_width = 50;
- var pic_height = 50;
- var mix_cnt = 100;
- var mix_dir = 0;
- var childs_arr = [[]];
- var childs_count = 0;
- function SlidingPuzzle()
- {
- super();
- if(this.autoStart)
- {
- this.start();
- }
- }
- function set picLinkage(picL)
- {
- this._picLinkage = picL;
- }
- function get picLinkage()
- {
- return this._picLinkage;
- }
- function set puzzleRezH(rn)
- {
- this._puzzleRezH = Math.max(Math.floor(rn),2);
- this._puzzleRezH = Math.min(this._puzzleRezH,12);
- }
- function get puzzleRezH()
- {
- return this._puzzleRezH;
- }
- function set puzzleRezV(rn)
- {
- this._puzzleRezV = Math.max(Math.floor(rn),2);
- this._puzzleRezV = Math.min(this._puzzleRezV,12);
- }
- function get puzzleRezV()
- {
- return this._puzzleRezV;
- }
- function set hintStyle(hnSt)
- {
- if(hnSt == "count" || hnSt == "matrix")
- {
- this._hintStyle = hnSt;
- }
- else
- {
- this._hintStyle = "none";
- }
- this.set_hint_style();
- }
- function get hintStyle()
- {
- return this._hintStyle;
- }
- function get puzzleDone()
- {
- return this._puzzleDone;
- }
- function startPuzzle(picLn, hr, vr, auM, hnS)
- {
- if(picLn != undefined)
- {
- this.picLinkage = picLn;
- }
- if(hr != undefined)
- {
- this.puzzleRezH = hr;
- }
- if(vr != undefined)
- {
- this.puzzleRezV = vr;
- }
- if(auM != undefined)
- {
- this.autoMix = auM;
- }
- if(hnS != undefined)
- {
- this.hintStyle = hnS;
- }
- this.clear_stage();
- this.init_picture();
- }
- function start()
- {
- this.clear_stage();
- this.init_picture();
- }
- function mixPuzzle(steps)
- {
- if(steps > 0)
- {
- this.mix_cnt = Number(steps);
- }
- if(this.mix_cnt > 0)
- {
- if(this.onEnterFrame != this.mixPuzzle)
- {
- this.onEnterFrame = this.mixPuzzle;
- }
- else
- {
- this.mix_cnt = this.mix_cnt - 1;
- this.mix_dir = this.mix_dir + 1;
- if(this.mix_dir > 1)
- {
- this.mix_dir = 0;
- }
- var _loc3_ = 2 * this.mix_dir + random(2);
- var _loc6_ = this.moveRandom(_loc3_);
- }
- }
- else
- {
- super.dispatchEvent({type:"mixed",target:this});
- delete this.onEnterFrame;
- }
- }
- function build_puzzle(Void)
- {
- this.init_params();
- this.piece_width = this.pic_width / this.rez_hrz;
- this.piece_height = this.pic_height / this.rez_vrt;
- this.childs_arr = [[]];
- var _loc25_ = (this.stage_width - this.pic_width) / 2;
- var _loc24_ = (this.stage_height - this.pic_height) / 2;
- var _loc9_ = 0;
- var _loc3_ = 0;
- while(_loc3_ < this.rez_vrt)
- {
- this.childs_arr[_loc3_] = [];
- var _loc2_ = 0;
- while(_loc2_ < this.rez_hrz)
- {
- var _loc8_ = _loc2_ * this.piece_width;
- var _loc7_ = _loc3_ * this.piece_height;
- var _loc5_ = {x:_loc8_ + 1,y:_loc7_ + 1,w:this.piece_width - 2,h:this.piece_height - 2,I:_loc2_,J:_loc3_};
- var _loc6_ = {x:_loc25_,y:_loc24_,I:_loc2_,J:_loc3_,active:true,Iorj:_loc2_,Jorj:_loc3_,Hcount:_loc9_,w:this.piece_width,h:this.piece_height};
- var _loc4_ = "pcs_" + _loc3_ + "_" + _loc2_;
- this.createClassObject(someFrogs.Puzzles.SlidingGroup,_loc4_,100 + _loc3_ * this.rez_hrz + _loc2_,{picLinkage:this._picLinkage,edgesArr:this._edgesArr,piecesArr:[_loc5_],groupProps:_loc6_});
- this.childs_arr[_loc3_][_loc2_] = this[_loc4_];
- _loc9_ = _loc9_ + 1;
- this.emptySpace = {I:_loc2_,J:_loc3_};
- _loc2_ = _loc2_ + 1;
- }
- _loc3_ = _loc3_ + 1;
- }
- this.missPiece(true);
- this.boundingBox_mc.useHandCursor = false;
- this.boundingBox_mc.onPress = function()
- {
- this._parent.childPressed();
- };
- if(this.autoMix)
- {
- this.mixPuzzle(5 * this.childs_count);
- }
- }
- function moveRandom(n)
- {
- var _loc3_ = n - 4 * Math.floor(n / 4);
- var _loc2_ = false;
- if(_loc3_ == 0)
- {
- _loc2_ = !this.movePiece(this.childs_arr[this.emptySpace.J - 1][this.emptySpace.I]) ? this.movePiece(this.childs_arr[this.emptySpace.J + 1][this.emptySpace.I]) : true;
- }
- else if(_loc3_ == 1)
- {
- _loc2_ = !this.movePiece(this.childs_arr[this.emptySpace.J + 1][this.emptySpace.I]) ? this.movePiece(this.childs_arr[this.emptySpace.J - 1][this.emptySpace.I]) : true;
- }
- else if(_loc3_ == 2)
- {
- _loc2_ = !this.movePiece(this.childs_arr[this.emptySpace.J][this.emptySpace.I - 1]) ? this.movePiece(this.childs_arr[this.emptySpace.J][this.emptySpace.I + 1]) : true;
- }
- else if(_loc3_ == 3)
- {
- _loc2_ = !this.movePiece(this.childs_arr[this.emptySpace.J][this.emptySpace.I + 1]) ? this.movePiece(this.childs_arr[this.emptySpace.J][this.emptySpace.I - 1]) : true;
- }
- return _loc2_;
- }
- function childPressed(obj)
- {
- if(this.getFocus() != this)
- {
- this.setFocus();
- }
- this.restoreActObj();
- this.promoteActObj(obj);
- }
- function restoreActObj(Void)
- {
- var _loc2_ = this.getInstanceAtDepth(this.top_depth);
- if(_loc2_ != undefined || _loc2_ != null)
- {
- _loc2_.swapDepths(_loc2_.ini_depth);
- _loc2_.activeColor(false);
- }
- }
- function promoteActObj(obj)
- {
- if(obj != undefined || obj != null)
- {
- obj.swapDepths(this.top_depth);
- obj.activeColor(true);
- }
- }
- function childReleased(obj)
- {
- if(this._puzzleDone != true)
- {
- if(this.movePiece(obj))
- {
- super.dispatchEvent({type:"moved",target:this});
- this.see_if_done();
- }
- }
- }
- function see_if_done(Viod)
- {
- if(this._puzzleDone)
- {
- return undefined;
- }
- this._puzzleDone = true;
- var _loc5_ = 0;
- while(_loc5_ < this.rez_vrt)
- {
- var _loc4_ = 0;
- while(_loc4_ < this.rez_hrz)
- {
- var _loc3_ = this.childs_arr[_loc5_][_loc4_];
- if(_loc3_ != null)
- {
- if(_loc3_.groupProps.I != _loc3_.groupProps.Iorj || _loc3_.groupProps.J != _loc3_.groupProps.Jorj)
- {
- this._puzzleDone = false;
- }
- }
- _loc4_ = _loc4_ + 1;
- }
- _loc5_ = _loc5_ + 1;
- }
- if(this._puzzleDone == true)
- {
- this.missPiece(false);
- super.dispatchEvent({type:"done",target:this});
- }
- }
- function missPiece(bl)
- {
- if(bl == false)
- {
- this.childs_arr[this.emptySpace.J][this.emptySpace.I]._visible = true;
- this.childs_arr[this.emptySpace.J][this.emptySpace.I].active = true;
- this.childs_arr[this.emptySpace.J][this.emptySpace.I].setEnabled(this.enabled);
- }
- else
- {
- this.childs_arr[this.emptySpace.J][this.emptySpace.I]._visible = false;
- this.childs_arr[this.emptySpace.J][this.emptySpace.I].active = false;
- this.childs_arr[this.emptySpace.J][this.emptySpace.I].setEnabled(this.enabled);
- }
- }
- function movePiece(obj)
- {
- var _loc8_ = false;
- if(obj == undefined || obj == null)
- {
- return false;
- }
- if(obj.groupProps.I == undefined || obj.groupProps.I == null)
- {
- return false;
- }
- if(obj.groupProps.J == undefined || obj.groupProps.J == null)
- {
- return false;
- }
- var _loc7_ = Math.abs(obj.groupProps.I - this.emptySpace.I);
- var _loc10_ = Math.abs(obj.groupProps.J - this.emptySpace.J);
- _loc8_ = _loc7_ == 0 && _loc10_ == 1 || _loc7_ == 1 && _loc10_ == 0;
- if(_loc8_)
- {
- var _loc4_ = this.emptySpace.I;
- var _loc3_ = this.emptySpace.J;
- var _loc6_ = obj.groupProps.I;
- var _loc5_ = obj.groupProps.J;
- var _loc9_ = this.childs_arr[_loc3_][_loc4_];
- _loc9_.put_me_to(_loc5_,_loc6_);
- obj.put_me_to(_loc3_,_loc4_);
- this.childs_arr[_loc5_][_loc6_] = _loc9_;
- this.childs_arr[_loc3_][_loc4_] = obj;
- this.emptySpace.I = _loc6_;
- this.emptySpace.J = _loc5_;
- }
- return _loc8_;
- }
- function clear_stage(Void)
- {
- this._puzzleDone = false;
- this.restoreActObj();
- var _loc3_ = 0;
- while(_loc3_ < this.rez_vrt)
- {
- var _loc2_ = 0;
- while(_loc2_ < this.rez_hrz)
- {
- if(typeof this.childs_arr[_loc3_][_loc2_] == "movieclip")
- {
- this.destroyObject(this.childs_arr[_loc3_][_loc2_]._name);
- }
- _loc2_ = _loc2_ + 1;
- }
- _loc3_ = _loc3_ + 1;
- }
- }
- function set_hint_style(Void)
- {
- var _loc3_ = 0;
- while(_loc3_ < this.rez_vrt)
- {
- var _loc2_ = 0;
- while(_loc2_ < this.rez_hrz)
- {
- if(typeof this.childs_arr[_loc3_][_loc2_] == "movieclip")
- {
- this.childs_arr[_loc3_][_loc2_].set_hint_style();
- }
- _loc2_ = _loc2_ + 1;
- }
- _loc3_ = _loc3_ + 1;
- }
- }
- function init_picture(Void)
- {
- if(this.puzzlePic_mc != undefined)
- {
- this.destroyObject("puzzlePic_mc");
- }
- this.createObject(this._picLinkage,"puzzlePic_mc",0);
- if(typeof this.puzzlePic_mc == "movieclip")
- {
- this.set_pic_size(this.puzzlePic_mc._width,this.puzzlePic_mc._height);
- this.build_puzzle();
- }
- else
- {
- this.createEmptyObject("puzzlePic_mc",0);
- var _loc2_ = new MovieClipLoader();
- this.loadListener = new Object();
- this.loadListener.controller = this;
- this.loadListener.onLoadInit = function(target_mc)
- {
- this.controller.set_pic_size(target_mc._width,target_mc._height);
- this.controller.build_puzzle();
- };
- this.loadListener.onLoadError = function(target_mc, errorCode)
- {
- this._picLinkage = "";
- this.controller.set_pic_size();
- this.controller.build_puzzle();
- };
- _loc2_.addListener(this.loadListener);
- _loc2_.loadClip(this._picLinkage,this.puzzlePic_mc);
- }
- }
- function set_pic_size(w, h)
- {
- this.pic_width = w == undefined ? 0.6 * this.stage_width : w;
- this.pic_height = h == undefined ? 0.6 * this.stage_height : h;
- this.puzzlePic_mc._visible = false;
- if(this.pic_width > this.stage_width || this.pic_height > this.stage_height)
- {
- this.setSize(this.pic_width,this.pic_height);
- }
- }
- function init_params(Void)
- {
- if(this.pic_width > this.pic_height)
- {
- this.rez_hrz = Math.max(this._puzzleRezH,this._puzzleRezV);
- this.rez_vrt = Math.min(this._puzzleRezH,this._puzzleRezV);
- }
- else
- {
- this.rez_vrt = Math.max(this._puzzleRezH,this._puzzleRezV);
- this.rez_hrz = Math.min(this._puzzleRezH,this._puzzleRezV);
- }
- this.childs_count = this.rez_vrt * this.rez_hrz;
- }
- function size(Void)
- {
- this.border_mc.move(0,0);
- this.border_mc.setSize(this.stage_width,this.stage_height);
- super.size();
- }
- function init(Void)
- {
- if(this.useSignature && this.frogPower_mc == undefined)
- {
- this.attachMovie("frogPower","frogPower_mc",2);
- }
- this.setSize(this._width,this._height);
- if(this.border_mc == undefined)
- {
- this.createClassObject(_global.styles.rectBorderClass,"border_mc",1,{styleName:this});
- }
- this.border_mc.setSize(this.stage_width,this.stage_height);
- super.init();
- mx.events.EventDispatcher.initialize(this);
- this.setEnabled(this.enabled);
- this.tabChildren = false;
- }
- function setSize(w, h)
- {
- this.stage_width = w;
- this.stage_height = h;
- this._xscale = this._yscale = 100;
- this.boundingBox_mc._alpha = 0;
- this.boundingBox_mc._width = w;
- this.boundingBox_mc._height = h;
- if(this.useSignature)
- {
- this.frogPower_mc._x = this.stage_width / 2 - 40;
- this.frogPower_mc._y = this.stage_height / 2 - 11;
- }
- var _loc3_ = 0;
- while(_loc3_ < this.rez_vrt)
- {
- var _loc2_ = 0;
- while(_loc2_ < this.rez_hrz)
- {
- _loc2_ = _loc2_ + 1;
- }
- _loc3_ = _loc3_ + 1;
- }
- this.size();
- }
- function setEnabled(enabled)
- {
- var _loc5_ = this.getStyle("disabledColor");
- this.disabled_color = {ra:Math.ceil(0.4 * (_loc5_ & 0xFF0000) / 65536),ga:Math.ceil(0.4 * (_loc5_ & 0xFF00) / 256),ba:Math.ceil(0.4 * (_loc5_ & 0xFF))};
- var _loc3_ = 0;
- while(_loc3_ < this.rez_vrt)
- {
- var _loc2_ = 0;
- while(_loc2_ < this.rez_hrz)
- {
- if(typeof this.childs_arr[_loc3_][_loc2_] == "movieclip")
- {
- this.childs_arr[_loc3_][_loc2_].setEnabled(enabled);
- }
- _loc2_ = _loc2_ + 1;
- }
- _loc3_ = _loc3_ + 1;
- }
- this.invalidate();
- }
- function get width()
- {
- return this.stage_width;
- }
- function get height()
- {
- return this.stage_height;
- }
- function onKillFocus(newFocus)
- {
- super.onKillFocus();
- }
- }
-